var express = require('express');
var router = express.Router();
var db = require('../../mysqldb');
var fs = require('fs');
module.exports = function (app) {
    app.use('/admin/banner/', router);
};

//添加轮播图
router.post('/addlist', (req, res, next) => {
      db(`INSERT INTO Banner VALUES (null,'${req.body.Title}','${req.body.Sumary}','${req.body.Link}','${req.body.Picture}',${req.body.IsDisplay},${req.body.IsIndex},${req.body.Sort},'0')`,(err,rows)=>{
          if(err){
              res.send({res:false})
          }
          res.send({ret:true})
      });
})

//修改轮播图
router.post('/update',(req, res, next) => {
    db(`UPDATE Banner SET Title='${req.body.Title}',Picture='${req.body.Picture}',Sort=${req.body.Sort},Link='${req.body.Link}',Sumary='${req.body.Sumary}',IsDisplay=${req.body.IsDisplay},IsIndex=${req.body.IsIndex} where id=${req.body.id}`,(err,rows)=>{
      if(err){
          res.send({ret:false})
      }
      res.send({ret:true})
    });
    
})
//展示一个
router.post('/edit', async (req, res, next) => { 
    db(`select * from Banner order by Sort where id= ${req.body.id}`,(err,rows)=>{
      if(err){
        res.send({ret:false})
      }
        res.send({
          ret:true,
          data:rows[0]
        })
    });
})

//查询轮播图
router.post('/show', async (req, res, next) => { 
    db('select * from Banner order by Sort desc',(err,rows)=>{
      var offset= req.body.offset?req.body.offset:0;
      var limit = req.body.limit?req.body.limit:10;
      if(err){
        res.send({ret:false})
      }
        res.send({
          ret:true,
          total:rows.length,
          data:rows.splice(offset,limit)
        })
    });
})

//删除轮播图
router.post('/del', async (req, res, next) => {
    db(`DELETE  from Banner where id='${req.body.id}'`,(err,rows)=>{
      if(err){
          res.send({ret:false})
      }
      res.send({ret:true})
    });
})